#include "BestQuoteDlg.h"

#include <core/FileSys.h>
#include <core/time/system_time.h>
#include <uam/ServerTimeMgr.h>
#include <widgets/SDockingButton.h>
#include <widgets/SSplitter.h>

#include <QFile>
#include <QHBoxLayout>
#include <QSettings>
#include <QVBoxLayout>

#include "BestQuoteDeal.h"
#include "BestQuoteFilter/BestQuoteFilter.h"
#include "BestQuoteFilter/BestQuoteFilterContainer.h"
#include "BestQuoteModel.h"
#include "BestQuoteTable.h"
#include "SetBrokerSortDlg.h"

BestQuoteDlg::BestQuoteDlg(QWidget* parent) : QWidget(parent) {
  QHBoxLayout* mainLayout = new QHBoxLayout(this);
  mainLayout->setContentsMargins(0, 0, 0, 0);
  mainLayout->setSpacing(4);

  m_filter = new BestQuoteFilterContainer(this);
  m_filter->setFixedWidth(398);
  mainLayout->addWidget(m_filter);

  m_splitter = new SSplitter(Qt::Vertical, this);
  m_splitter->setOpaqueResize(false);
  m_splitter->setHandleWidth(4);
  mainLayout->addWidget(m_splitter);

  m_table = new BestQuoteTable(this);
  m_splitter->addWidget(m_table);

  m_deal = new BestQuoteDeal(this);
  m_deal->setMinimumHeight(28);
  m_splitter->addWidget(m_deal);
  m_splitter->setStretchFactor(0, 2);
  m_splitter->setStretchFactor(1, 1);

  SDockingButton* dock = new SDockingButton(m_filter, Qt::AlignLeft, this);

  connect(m_table, &BestQuoteTable::brokerChanged, m_deal,
          &BestQuoteDeal::onBrokerTabChanged);
  connect(m_deal, &BestQuoteDeal::collapsed, this,
          &BestQuoteDlg::onDealCollapsed);
  connect(m_splitter, &QSplitter::splitterMoved,
          [=]() { m_deal->setCollapsed(false); });

  connect(m_filter->getFilter(), &BestQuoteFilter::filterChange, this,
          &BestQuoteDlg::onFilterChange);
  connect(m_filter, &BestQuoteFilterContainer::filterChange, this,
          &BestQuoteDlg::onFilterChange);

  displayBrokerSortDlg();
}

CBrokerUnit BestQuoteDlg::getCurBroker() {
  CBrokerUnit unit;
  if (m_table->currentModel()) unit = m_table->currentModel()->GetBroker();
  return unit;
}

FilterCondition BestQuoteDlg::getCurFilter() {
  FilterCondition tmp;
  if (m_table->currentModel()) tmp = m_table->currentModel()->GetFilter();
  return tmp;
}

FilterCondition& BestQuoteDlg::getFilterCondition() {
  return m_filter->getFilterCondition();
}

bool BestQuoteDlg::selValidCompany() {
  return m_table->currentModel() != nullptr;
}

void BestQuoteDlg::displayBrokerSortDlg() {
  QSettings settings(
      FileSys::instance()->writeableAppCfgDir() + "/globalsettings.ini",
      QSettings::IniFormat);
  settings.beginGroup("User");
  bool firstRun = settings.value("FirstRun", "").toBool();
  settings.endGroup();

  if (!firstRun) return;

  bool bValid = false;
  {
    const CBrokerList& brokerList =
        SSUserAccountManager::instance().GetReadonlyBroker();
    CBrokerList::BrokerMap::const_iterator itor =
        brokerList.GetBrokerMap().begin();
    for (; itor != brokerList.GetBrokerMap().end(); itor++) {
      const CBrokerUnit& stUnit = itor->second;
      if (!CBrokerList::IsValidBroker(stUnit.m_brokerID)) continue;
      if (!stUnit.m_enableFlag_BOND && !stUnit.m_enableFlag_NCD) continue;
      bValid = true;
      break;
    }
  }

  if (bValid) {
    //	qb::base::CTime ctime(ServerTimeMgr::instance().serverTime());
    //	int nCurTime, nSavTime;
    //	nCurTime = ctime.GetYear() * 100 + ctime.GetMonth();
    //	nSavTime = firstRunValid;
    //	if (nCurTime > nSavTime)
    {
      SetBrokerSortDlg* brokerSortDlg = new SetBrokerSortDlg(this);
      brokerSortDlg->exec();
    }

    QSettings settings(
        FileSys::instance()->writeableAppCfgDir() + "/globalsettings.ini",
        QSettings::IniFormat);
    settings.beginGroup("User");
    settings.setValue("FirstRun", false);
    settings.endGroup();
  }
}

void BestQuoteDlg::onDealCollapsed(bool v) {
  if (v) {
    m_prevousHeight = m_splitter->sizes();
    QList<int> sizes;
    sizes << m_splitter->geometry().height() - 4 - 28 << 28;
    m_splitter->setSizes(sizes);
  } else {
    m_splitter->setSizes(m_prevousHeight);
  }
}

void BestQuoteDlg::onFilterChange() { emit filterChange(); }
